<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>for循环练习</title>
</head>

<body>
  <script>
    // for 循环练习

    // 1. 页面打印出5颗小星星 ★★★★★
    for (let i = 1; i <= 5; i++) {
      document.write('★')
    }


    // 2. 页面输出年龄 (1~100岁)

    for (let age = 1; age <= 100; age++) {
      document.write(`<br>我的年龄是${age}岁`)
    }

    // 3. 页面输出1~100以内的偶数

    for (let even = 1; even <= 100; even++) {
      if (even % 2 == 0) {
        document.write(`<br>输出偶数${even}`)
      }

    }


    // 4. 求 1~100 之间的累加和 （拿着前面 累加的值 和 下一个数字 相加）
    // // 4.1 准备一个累加和的变量用来存储结果
    // // 4.2 开始循环累加

    let sum = 0
    for (let num = 1; num <= 100; num++) {
      sum += num
    }
    document.write(sum)

    // 5. 求 1~100 之间的所有偶数的和
    let sum1 = 0
    for (let num1 = 1; num1 <= 100; num1++) {
      if (num1 % 2 == 0) {
        sum1 += num1
      }
    }
    document.write(sum1)
    
    let sum2 = 0
    for (let num1 = 1; num1 <= 100; num1++) {
      if (num1 % 2 !== 0) {
        sum2 += num1
      }
    }
    document.write(sum2)



  </script>
</body>

</html>